﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Messaging;

using System.Xml;
using System.Xml.Schema;
using System.Xml.Xsl;

using System.Web.UI;
using System.Diagnostics;

namespace GetMsg
{
    class Program
    {
        static void Main()
        {
            createXSLT(); //---->> For Login (one time upload of XSLT for trader) Trader.sln -> TradingClient.cs -> createXSLT
            //msgSchema();

            //Create XML Doc String //---->> For Broadcasting (constant update on Matched Orders) OME.sln -> QuoteServer.cs -> getMsg
            MessageQueue tQueue = new MessageQueue(@".\Private$\TradeQueue");
            tQueue.Formatter = new XmlMessageFormatter(new string[] { "System.String" });

            MessageEnumerator qEnum = tQueue.GetMessageEnumerator2();


            String xmlMsg = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?><TradeQueue>";

            while (qEnum.MoveNext())
            {
                xmlMsg += qEnum.Current.Body.ToString();
            }

            xmlMsg += "</TradeQueue>";

            //Create actual XML Doc and Display in Browser //---->> For Trader to View Trader.sln -> TradingClient.cs -> displayXML
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(xmlMsg);
            
            doc.Save("C:\\Temp\\A1Exchange.xml");

            //doc.Save(Console.Out);
            //Console.WriteLine(xmlMsg);

            //Console.ReadLine();
            

            XslTransform tranXml = new XslTransform();
            tranXml.Load("C:\\Temp\\A1Exchange.xslt");
            
            //XmlWriter writer = new XmlTextWriter(Console.Out);

            tranXml.Transform("C:\\Temp\\A1Exchange.xml", "C:\\Temp\\A1Exchange.html");
            Process.Start("C:\\Temp\\A1Exchange.html");
        }

        private static void createXSLT()
        {
            string xsltMsg = "<?xml version = \"1.0\" encoding=\"ISO-8859-1\" ?>";

            xsltMsg += "<xsl:stylesheet version=\"1.0\" xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\">";
            xsltMsg += "<xsl:output method=\"html\" />";
            xsltMsg += "<xsl:template match=\"/\">";
            xsltMsg += "<html><body><h2>Trade Printout</h2>";
            xsltMsg += "<table><tr><th>Buy ID</th><th>Sell ID</th><th>Quantity</th><th>Price</th></tr>";
            xsltMsg += "<xsl:for-each select=\"TradeQueue/Trade\">";
            xsltMsg += "<tr><td><xsl:value-of select=\"BuyID\"/></td><td><xsl:value-of select=\"SellID\"/></td><td><xsl:value-of select=\"Quantity\"/></td><td><xsl:value-of select=\"Price\"/></td></tr>";
            xsltMsg += "</xsl:for-each></table></body></html></xsl:template></xsl:stylesheet>";

            System.IO.File.WriteAllText(@"C:\\Temp\\A1Exchange.xslt", xsltMsg);
        }

        //protected override void Render(System.Web.UI.HtmlTextWriter writer)
        //{
        //    System.Web.UI.WebControls.Xml msgXml = new System.Web.UI.WebControls.Xml();
        //    //msgXml.DocumentSource;
        //    //msgXml.RenderControl(writer);

        //    base.Render(writer);
        //}

        private static void msgSchema()
        {
            XmlSchema schema = new XmlSchema();

            XmlSchemaElement elBuyID = new XmlSchemaElement();
            schema.Items.Add(elBuyID);
            elBuyID.Name = "BuyID";
            elBuyID.SchemaTypeName = new XmlQualifiedName("long", "http://www.w3.org/2001/XMLSchema");

            XmlSchemaElement elSellID = new XmlSchemaElement();
            schema.Items.Add(elSellID);
            elSellID.Name = "SellID";
            elSellID.SchemaTypeName = new XmlQualifiedName("long", "http://www.w3.org/2001/XMLSchema");

            XmlSchemaElement elQuant = new XmlSchemaElement();
            schema.Items.Add(elQuant);
            elQuant.Name = "Quantity";
            elQuant.SchemaTypeName = new XmlQualifiedName("int", "http://www.w3.org/2001/XMLSchema");

            XmlSchemaElement elPrice = new XmlSchemaElement();
            schema.Items.Add(elPrice);
            elPrice.Name = "Price";
            elPrice.SchemaTypeName = new XmlQualifiedName("double", "http://www.w3.org/2001/XMLSchema");

            // <xs:element name="TradeQueue">
            XmlSchemaElement elTradeQ = new XmlSchemaElement();
            schema.Items.Add(elTradeQ);
            elTradeQ.Name = "TradeQueue";

            // <xs:complexType>
            XmlSchemaComplexType complexTypeQ = new XmlSchemaComplexType();
            elTradeQ.SchemaType = complexTypeQ;

            // <xs:all minOccurs="1" maxOccurs="unbounded">
            XmlSchemaAll allQ = new XmlSchemaAll();
            complexTypeQ.Particle = allQ;
            allQ.MinOccurs = 1;
            allQ.MaxOccursString = "unbounded";

            // <xs:element name="Trade">
            XmlSchemaElement elTrade = new XmlSchemaElement();
            schema.Items.Add(elTrade);
            elTrade.Name = "Trade";

            // <xs:complexType>
            XmlSchemaComplexType complexType = new XmlSchemaComplexType();
            elTrade.SchemaType = complexType;

            // <xs:all minOccurs="1" maxOccurs="unbounded">
            XmlSchemaAll allT = new XmlSchemaAll();
            complexType.Particle = allT;
            allT.MinOccurs = 1;
            allT.MaxOccursString = "unbounded";

            // <xs:element ref="BuyID"/>
            XmlSchemaElement buyRef = new XmlSchemaElement();
            allT.Items.Add(buyRef);
            buyRef.RefName = new XmlQualifiedName("BuyID");

            // <xs:element ref="SellID"/>
            XmlSchemaElement sellRef = new XmlSchemaElement();
            allT.Items.Add(sellRef);
            sellRef.RefName = new XmlQualifiedName("SellID");

            // <xs:element ref="Quantity"/>
            XmlSchemaElement quantRef = new XmlSchemaElement();
            allT.Items.Add(quantRef);
            buyRef.RefName = new XmlQualifiedName("Quantity");

            // <xs:element ref="Price"/>
            XmlSchemaElement priceRef = new XmlSchemaElement();
            allT.Items.Add(priceRef);
            priceRef.RefName = new XmlQualifiedName("Price");

            schema.Compile(new ValidationEventHandler(ValidationCallbackOne));
            XmlNamespaceManager nsmgr = new XmlNamespaceManager(new NameTable());
            nsmgr.AddNamespace("xs", "http://www.w3.org/2001/XMLSchema");
            schema.Write(Console.Out, nsmgr);
        }

        public static void ValidationCallbackOne(object sender, ValidationEventArgs args)
        {
            Console.WriteLine(args.Message);
        }
    }
}
